﻿using System;
using System.Data;
using System.Configuration;
using System.Diagnostics;
using System.Linq;
using NENAData.LINQ;
using NENAData.NENAProvinces;

public class NVPConnection
    {

    private static TraceSource _source = new TraceSource("PayPal");

        //private string APIUsername = ConfigurationManager.AppSettings["APIUserName"];
        //private string APIPassword = ConfigurationManager.AppSettings["APIPassword"];
        //private string APISignature = ConfigurationManager.AppSettings["APISignature"];
        private string Subject = ConfigurationManager.AppSettings["PayPalSubject"];

        /// <summary>
        /// Credentials added to the NVP string
        /// </summary>
        /// <returns></returns>
        public string buildCredentialsNVPString(int intAffiliate)
        {
            NVPCodec codec = new NVPCodec();
            NENAAffiliate oAPI = new NENAAffiliate();

            if (intAffiliate == 99)
            {
                //set to Loca Fitness
                codec["USER"] = "corleen.scheidl_api1.gmail.com";
                codec["PWD"] = "SZM4R7CSHWBEBVG9";
                codec["SIGNATURE"] = "AenJJ73PIJUF5-IZUXsHw4nZ8lxdAmNFE7VHLPkg.u8sJYicCnvQcf50";
            }
            else
            {
                Affiliate oAffiliate = oAPI.GetAffiliate_byAffiliateID(intAffiliate);

                _source.TraceEvent(TraceEventType.Information, 0, "buildCredentialsNVPString PayPalAPIUserName : " + oAffiliate.PayPalAPIUserName);
                //_source.TraceEvent(TraceEventType.Information, 0, "buildCredentialsNVPString PayPalAPIPassword : " + oAffiliate.PayPalAPIPassword);
                //_source.TraceEvent(TraceEventType.Information, 0, "buildCredentialsNVPString SIGNATURE : " + oAffiliate.PayPalAPISignature);
                _source.TraceEvent(TraceEventType.Information, 0, "buildCredentialsNVPString Affiliate_Full : " + oAffiliate.Affiliate_Full);

                codec["USER"] = oAffiliate.PayPalAPIUserName;
                codec["PWD"] = oAffiliate.PayPalAPIPassword;
                codec["SIGNATURE"] = oAffiliate.PayPalAPISignature;
            }

            if (!IsEmpty(Subject))
                codec["SUBJECT"] = Subject;

            //static version based on NVP
            codec["VERSION"] = "72.0";

            return codec.Encode();
        }

        /// <summary>
        /// Returns if a string is empty or null
        /// </summary>
        /// <param name="s">the string</param>
        /// <returns>true if the string is not null and is not empty or just whitespace</returns>
        public static bool IsEmpty(string s)
        {
            return s == null || s.Trim() == string.Empty;
        }

    }
